<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo">
        <img src="@/assets/MallLogin.png" alt="" />
        电商后台管理系统
      </div>
      <el-button type="info" @click="exitLogin">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 主体侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409BFF" :unique-opened="true" mode="vertical" :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menusList" :key="item.id">
            <!-- 一级菜单的模版区域 -->
            <template slot="title">
              <!-- 一级菜单的图标 -->
              <i :class="iconObj[item.id]" class="menus-icon"></i>
              <!-- 一级菜单的文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path + ''" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <!-- 二级菜单的模版区域 -->
              <template slot="title">
                <!-- 二级菜单的图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单的文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 用于存放左侧菜单列表数据
      menusList: [],
      // 字体图标---侧边栏菜单权限列表每一项的ID所对应的字体图表
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 用于控制菜单栏是否折叠起来----默认不折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  methods: {
    // 点击了退出登录按钮
    exitLogin() {
      // 清空 token 值
      sessionStorage.removeItem('token')
      sessionStorage.removeItem('activePath')
      // 跳转到 登录页面 /login
      this.$router.push('/login')
      this.$message.success('退出成功。欢迎下次光临~')
    },
    // 获取左侧 侧边栏菜单权限列表
    async initMenusList() {
      const { data: res } = await this.$http({
        method: 'GET',
        url: '/menus'
      })
      // 获取数据失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 将数据转存到 data 数据源中
      this.menusList = res.data
    },
    // 点击按钮切换  菜单栏的展开或折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存当前点击二级菜单的 路由地址,保存链接的激活状态
    saveNavState(activePath) {
      this.activePath = activePath
      window.sessionStorage.setItem('activePath', activePath)
    }
  },
  created() {
    this.initMenusList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #373d41;
    padding-left: 0;
    .logo {
      display: flex;
      align-items: center;
      font-size: 20px;
      color: #fff;
      img {
        width: 61px;
        height: 56px;
        margin-right: 10px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      letter-spacing: 0.025rem;
      color: #fff;
      text-align: center;
      cursor: pointer;
    }
    .el-menu {
      border: 0;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
.menus-icon {
  margin-right: 8px;
}
</style>
